﻿using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Aspose.Excel;

namespace LenderClearingHouse
{
    public partial class ProjectManagerList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            listFilter.SearchButton.Click += new ImageClickEventHandler(SearchButton_Click);
        }

        protected override void OnInit(EventArgs e)
        {
          base.OnInit(e);
        }

        void SearchButton_Click(object sender, ImageClickEventArgs e)
        {
            grid.Rebind();
        }

        protected void dsLvreo_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@Apn"].Value = listFilter.Apn;
            e.Command.Parameters["@PropLocation"].Value = listFilter.Address;
            e.Command.Parameters["@Zip"].Value = listFilter.Zip;
            e.Command.Parameters["@PMId"].Value = (Session["User"] as User).UserId;
        }

        protected void grid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
          GridEditableItem editedItem = e.Item as GridEditableItem;
          ProjectManagerEditControl userControl = (ProjectManagerEditControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

          DataAccess da = new DataAccess();
          User u = Session["User"] as User;

          da.ExecuteProcedure("updatePMList",
              new string[]
                {
                    "@PropertyId", "@ActualCompDate", "@NotesStatus", "@NotesStatusDate", "@NotesStatusUser", "@EstRehabCompDate"
                },
              new object[]
                {
                    (long)editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["PropertyId"],
                    userControl.edActualCompDate.DbSelectedDate,
                    userControl.edNotesStatus.Text, DateTime.Now, u.FName + " " + u.LName, 
                    userControl.edEstRehabCompDate.DbSelectedDate
                });

        }
      
        protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                GridPagerItem item = e.Item as GridPagerItem;
                RadComboBox combo = (RadComboBox)item.FindControl("PageSizeComboBox");

                RadComboBoxItem newItem = combo.Items.FindItemByValue("100");
                if (newItem == null)
                {
                    RadComboBoxItem citem = new RadComboBoxItem("100", "100");
                    citem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
                    combo.Items.Add(citem);
                }
                else
                {
                    newItem.Text = "100";
                }

                newItem = combo.Items.FindItemByValue("1000000");
                if (newItem == null)
                {
                    RadComboBoxItem citem = new RadComboBoxItem("ALL", "1000000");
                    citem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
                    combo.Items.Add(citem);
                }
                else
                {
                    newItem.Text = "ALL";
                }
            }
            else if (e.Item is GridDataItem)
            {
              GridDataItem itm = e.Item as GridDataItem;
              DataRowView drw = itm.DataItem as DataRowView;

              string notesStatus = "";
              if (drw["NotesStatus"] != DBNull.Value)
                notesStatus += drw["NotesStatus"].ToString();
              if (drw["NotesStatusDate"] != DBNull.Value)
                notesStatus += " (" + Convert.ToDateTime(drw["NotesStatusDate"]).ToString("MM/dd/yyyy hh:mm tt") + " " + drw["NotesStatusUser"].ToString() + ")";
              itm["NotesStatus"].Text = notesStatus;
            }
        }

        protected void lnkRefreshGrid_Click(object sender, EventArgs e)
        {
            grid.Rebind();
        }
    }
}